using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PostController : ControllerBase
    {
        private readonly IPostService _postService;

        public PostController(IPostService postService)
        {
            _postService = postService;
        }

        [HttpGet]
        public async Task<ActionResult<List<Post>>> GetAllPosts()
        {
            var posts = await _postService.GetActivePostsAsync();
            return Ok(posts);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Post>> GetPostById(Guid id)
        {
            var post = await _postService.GetByIdAsync(id);
            if (post == null || post.IsDeleted == true)
                return NotFound();

            // 增加浏览次数
            await _postService.IncrementViewCountAsync(id);
            return Ok(post);
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<List<Post>>> GetPostsByUserId(Guid userId)
        {
            var posts = await _postService.GetPostsByUserIdAsync(userId);
            return Ok(posts);
        }

        [HttpGet("category/{categoryId}")]
        public async Task<ActionResult<List<Post>>> GetPostsByCategoryId(Guid categoryId)
        {
            var posts = await _postService.GetPostsByCategoryIdAsync(categoryId);
            return Ok(posts);
        }

        [HttpGet("tag/{tagId}")]
        public async Task<ActionResult<List<Post>>> GetPostsByTagId(Guid tagId)
        {
            var posts = await _postService.GetPostsByTagIdAsync(tagId);
            return Ok(posts);
        }

        [HttpGet("search")]
        public async Task<ActionResult<List<Post>>> SearchPosts([FromQuery] string searchTerm)
        {
            var posts = await _postService.SearchPostsAsync(searchTerm);
            return Ok(posts);
        }

        [HttpPost]
        public async Task<ActionResult<Post>> CreatePost(Post post)
        {
            try
            {
                var createdPost = await _postService.CreateAsync(post);
                return CreatedAtAction(nameof(GetPostById), new { id = createdPost.PostId }, createdPost);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<Post>> UpdatePost(Guid id, Post post)
        {
            if (id != post.PostId)
                return BadRequest();

            try
            {
                var updatedPost = await _postService.UpdateAsync(post);
                return Ok(updatedPost);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeletePost(Guid id)
        {
            try
            {
                var result = await _postService.DeleteAsync(id);
                if (!result)
                    return NotFound();

                return NoContent();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}/soft-delete")]
        public async Task<ActionResult<Post>> SoftDeletePost(Guid id)
        {
            try
            {
                var post = await _postService.SoftDeleteAsync(id);
                return Ok(post);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }
    }
} 